/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-03-03
*/
package com.rzico.basics.service;

import com.rzico.base.BaseMapper;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.basics.entity.Designer;
import com.rzico.basics.entity.Enterprise;
import com.rzico.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rzico.basics.mapper.DesignerMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <pre>
 * 设计器业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class DesignerService extends BaseServiceImpl<Designer, String> {

    @Autowired
    private DesignerMapper designerMapper;

    @Autowired
    private EnterpriseService enterpriseService;

    @Override
    public BaseMapper<Designer, String> getMapper() {
        return designerMapper;
    }

    @Transactional(rollbackFor = Exception.class)
    public Integer save(List<Designer> designerList) {

        if (designerList.size()==0) {
            throw new CustomException("没有组件");
        }

        Enterprise enterprise = enterpriseService.getCurrent();

        Designer designer = new Designer();
        designer.setEnterpriseId(enterprise.getId());
        designer.setPageId(designerList.get(0).getPageId());
        int affectCount = super.delete(designer);

        for (Designer d:designerList) {
            d.setEnterpriseId(enterprise.getId());
            d.setCreateDate(new Date());
            d.setModifyDate(new Date());
            super.insertUseGeneratedKeys(d);
            affectCount = affectCount + 1;
        }

        return affectCount;

    }

}
